package com.symaster.common.geom.util;

import com.symaster.common.geom.entity.Polygon;

import javax.swing.*;
import java.awt.*;

/**
 * @author yinmiao
 * @date 2021-09-08 18:51
 */
public class ShowPolygon extends JFrame {

    final Color color = new Color(30, 34, 38);
    private int width;
    private int height;
    private final Polygon[] polygon;
    private JPanel root;
    private JScrollPane jsp;
    private final MyPanel myPanel;

    public ShowPolygon(int width, int height, Polygon... polygon) {
        setPreferredSize(new Dimension(900, 600));
        this.width = width;
        this.height = height;
        this.polygon = polygon;
        this.setContentPane(root);
        this.myPanel = new MyPanel();

        int maxw = 0;
        int maxh = 0;
        for (Polygon polygon1 : polygon) {
            double w = polygon1.getX() + polygon1.getWidth();
            if (w > maxw) {
                maxw = (int) w + 1;
            }

            double h = polygon1.getY() + polygon1.getHeight();
            if (h > maxh) {
                maxh = (int) h + 1;
            }
        }
        this.width = maxw;
        this.height = maxh;

        jsp.setViewportView(myPanel);
        myPanel.setPreferredSize(new Dimension(this.width + 20, this.height + 20));
        root.setBackground(color);
        myPanel.setBackground(color);
        jsp.setBackground(color);
        myPanel.setLocation(20, 20);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
    }

    public static void main(String[] args) {
        ShowPolygon showPolygon = new ShowPolygon(300, 300, new Polygon(new double[]{0, 0, 100, 100}, new double[]{0, 100, 100, 0}));
        showPolygon.open();
    }

    public void open() {
        myPanel.repaint();
        repaint();
        root.repaint();
        setVisible(true);
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root = new JPanel();
        root.setLayout(new BorderLayout(0, 0));
        jsp = new JScrollPane();
        root.add(jsp, BorderLayout.CENTER);
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return root;
    }

    class MyPanel extends JPanel {

        private Image image;    // 图像缓冲
        private Graphics og;

        public void display() {

            setPreferredSize(new Dimension(width + 20, height + 20));

            if (og == null) {
                setBackground(color);
                image = this.createImage(width + 20, height + 20);
                if (image != null) {
                    og = image.getGraphics();
                }
            }

            if (og != null) {
                super.paint(og);

                for (Polygon pat : polygon) {
                    if (pat == null) {
                        System.err.println("pat == null");
                        continue;
                    }

                    double[] xpoints = pat.getXpoints();
                    double[] ypoints = pat.getYpoints();
                    int[] xpointsInt = new int[xpoints.length];
                    int[] ypointsInt = new int[xpoints.length];

                    for (int i = 0; i < xpoints.length; i++) {
                        xpointsInt[i] = (int) xpoints[i];
                        ypointsInt[i] = (int) ypoints[i];
                    }

                    java.awt.Polygon polygon = new java.awt.Polygon(xpointsInt, ypointsInt, pat.getXpoints().length);

                    // 填充绘制
                    // Color fillColor = new Color(116, 116, 116);
                    // og.setColor(fillColor);
                    // og.fillPolygon(polygon);

                    // 边框绘制
                    final Color borderColor = new Color(206, 158, 158);
                    og.setColor(borderColor);
                    og.drawPolygon(polygon);

                    // 点坐标绘制
                    Color fillColor = new Color(101, 253, 0);
                    og.setColor(fillColor);
                    int[] xpoints1 = polygon.xpoints;
                    int[] ypoints1 = polygon.ypoints;
                    for (int i = 0; i < xpoints1.length; i++) {
                        og.drawRect(xpoints1[i] - 1, ypoints1[i] - 1, 2, 2);
                    }
                }
            }
        }

        public void paint(Graphics g) {
            super.repaint();
            display();
            g.drawImage(image, 0, 0, this);
        }
    }
}
